import {Component, Input} from '@angular/core';
import {
    AlertController, IonicPage, LoadingController, ModalController, NavController, NavParams, Platform,
    ToastController
} from 'ionic-angular';
import {BasePage} from "../../BasePage";
import {Log} from "../../../framework/utils/Log";
import {SystemService} from "../../service/SystemService";
import {FileTransfer, FileUploadOptions, FileTransferObject} from '@ionic-native/file-transfer';
import {File} from '@ionic-native/file';
import {FileOpener} from "@ionic-native/file-opener";
import {ZjApprovePage} from "../zjInformation/zjApprove/zjApprove";
import {ShowpdfPage} from "../../showpdf/showpdf";
import { GalleryModal } from 'ionic-gallery-modal';
import {Utils} from "../../../framework/utils/Utils";
import {FilePreview} from "../../../framework/utils/FilePreview";

/**
 * Generated class for the InformationPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@Component({
    selector: 'page-cgInformation',
    templateUrl: 'cgInformation.html',
    providers: [SystemService]
})
export class CgInformationPage extends BasePage {
    spType: any;
    _d: any;
    detailItem: any=[];
    private static TAG: string = 'CgInformationPage';
    detailInfo: any;
    type: string;
    param:any;
    private photos: any[] = [];
    imageArray: any[] = [];
    private filePreview:any;
    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                public sys: SystemService,
                public platform: Platform,
                public loadingCtrl: LoadingController,
                private fileOpener: FileOpener,
                public fileTransfer: FileTransferObject,
                public toastCtrl: ToastController,
                public alertCtrl: AlertController,
                public modalCtrl:ModalController,

                private file: File) {
        super(platform, loadingCtrl, toastCtrl, alertCtrl);
        this.detailItem = navParams.data.data;
        this.type = this.navParams.data.type;
        this.spType =this.navParams.data.spType;
        this._d = this.navParams.data._d;
        this.filePreview = new FilePreview(loadingCtrl,platform,fileTransfer,file,fileOpener);
    }

    ionViewDidEnter(){
    }
    sqfjHandler(item) {

        //文件路径
        var filePath = item.FuJianLJ;
        //获取最后一个.的位置
        var index= filePath.lastIndexOf(".");
        //获取后缀
        var ext = filePath.substr(index+1);
        if(ext ==='pdf'){
            this.checkUrl(filePath);
        }else if(Utils.isAssetTypeAnImage(ext)) {
            this.imageArray.push(item.FuJianLJ);
            this.openModal();
        }else{
            this.filePreview.download(item.FuJianMC,item.FuJianLJ,ext);

        }


    }
    checkUrl(url) {
        let modal = this.modalCtrl.create(ShowpdfPage, {
            url: url
        });
        modal.present();
    }
    openModal() {
        this.changeArrayToGallery(this.imageArray);
        let modal = this.modalCtrl.create(GalleryModal, {
            photos: this.photos,
            initialSlide: 1,
        });
        modal.present();
    }


    changeArrayToGallery(array) {
        for (let i = 0; i < array.length; i++) {
            var object = { "url": array[i]};
            this.photos.push(object);
        }
    }

    doClickHandler(){
        this.navCtrl.push(ZjApprovePage,{data:this.detailItem,_d:this._d,spType:this.spType})

    }
}


